package com;


import com.util.IsDark;
import javafx.collections.ObservableList;
import javafx.scene.layout.VBox;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;


public class MainListenController {

    String firstPicDir[] = new String[6];
    String secondPicDir[] = new String[6];
    String thirdPicDir[] = new String[29];
    String fourPicDir[] = new String[9];
    String statusDir[] = new String[9];

    InputStream statuDir[] = new InputStream[5];

    /**
     * 获取第一张图片
     */
    public void dirListener(String dir, VBox parentVBox) {
        VBox newVBox = getFirstThemeVBox(dir);
        //     VBox newVBox = getSecondThemeVBox(dir);
//        VBox newVBox = getThirdThemeVBox(dir);
//        VBox newVBox = getFourThemeVBox(dir);
        changeVbox(parentVBox, newVBox);
    }

    /**
     * 切换图片
     */
    public void switchPic(String dir, VBox parentVBox, int index) {
        if (index == 1) {
            VBox newVBox = getFirstThemeVBox(dir);
            changeVbox(parentVBox, newVBox);
        }
        if (index == 2) {
            VBox newVBox = getSecondThemeVBox(dir);
            changeVbox(parentVBox, newVBox);
        }
        if (index == 3) {
            VBox newVBox = getThirdThemeVBox(dir);
            changeVbox(parentVBox, newVBox);
        }
        if (index == 4) {
            VBox newVBox = getFourThemeVBox(dir);
            changeVbox(parentVBox, newVBox);
        }
    }

    public VBox getFirstThemeVBox(String dir) {
        //主图片
        for (int i = 0; i < 6; i++) {
            firstPicDir[i] = dir + "/" + ResName.firstPicResNames[i];
        }
        boolean isDark = checkDark(dir);
        VBox vBox = new ShowView().showFirstView(firstPicDir, statuDir, isDark);
        return vBox;
    }

    public VBox getSecondThemeVBox(String dir) {
        //主图片
        secondPicDir[0] = dir + "/" + ResName.firstPicResNames[0];
        secondPicDir[1] = dir + "/" + ResName.secondPicResName[0];
        secondPicDir[2] = dir + "/" + ResName.secondPicResName[1];
        secondPicDir[3] = dir + "/" + ResName.secondPicResName[2];
        secondPicDir[4] = dir + "/" + ResName.secondPicResName[3];
//        secondPicDir[5] = dir + "/" + ResName.firstPicResNames[5];

        //状态栏
        boolean isDark = checkDark(dir);
        VBox vBox = new ShowView().showSecondView(secondPicDir, statuDir,isDark);
        return vBox;
    }

    public VBox getThirdThemeVBox(String dir) {
        for (int i = 0; i < 29; i++) {
            thirdPicDir[i] = dir + ResName.thirdPicResName[i];
        }

//状态栏
        boolean isDark = checkDark(dir);
        VBox vBox = new ShowView().getThirdView(thirdPicDir, statuDir, isDark);
        return vBox;
    }

    public VBox getFourThemeVBox(String dir) {
        for (int i = 0; i < 9; i++) {
            fourPicDir[i] = dir + ResName.fourPicResName[i];
        }
//状态栏
        boolean isDark = checkDark(dir);
        VBox vBox = new ShowView().getfourView(fourPicDir, statuDir, isDark);
        return vBox;
    }


    /**
     * 删除旧节点，添加新节点
     *
     * @param parentVBox 展示框的父组件
     * @param newVBox    新组建，显示新预览图
     */
    private void changeVbox(VBox parentVBox, VBox newVBox) {
        ObservableList list = parentVBox.getChildren();
        parentVBox.getChildren().removeAll(list);
        parentVBox.getChildren().add(newVBox);
    }

//    public boolean checkDark(String dir) {
//        for (int i = 0; i < 9; i++) {
//            statusDir[i] = dir + "/" + ResName.firstPicResNames[i +6 ];
//        }
//        boolean isDark = false;
//        try {
//            if (IsDark.getRGB(ImageIO.read(new File(dir + "/" + ResName.firstPicResNames[14])), 10, 10)) {
////                statusDir[0] = "src/com/statue/signal_light.png";
////                statusDir[1] = "src/com/statue/time_light.png";
////                statusDir[2] = "src/com/statue/vivo_signal4_small_4_white.png";
////                statusDir[3] = "src/com/statue/wifi_light.png";
//
//                statusDir[0] = ShowView.class.getResourceAsStream("statue/signal_light.png").toString();
//                statusDir[1] = ShowView.class.getResourceAsStream("statue/time_light.png").toString();
//                statusDir[2] = ShowView.class.getResourceAsStream("statue/vivo_signal4_small_4_white.png").toString();
//                statusDir[3] = ShowView.class.getResourceAsStream("statue/wifi_light.png").toString();
//
//                statusDir[4] = dir + "/com.android.systemui/vivo_battery_20_19_white.png";
//                isDark = true;
//            } else {
//                statusDir[0] = "src/com/statue/signal_black.png";
//                statusDir[1] = "src/com/statue/time_black.png";
//                statusDir[2] = "src/com/statue/vivo_signal4_small_4_color.png";
//                statusDir[3] = "src/com/statue/wifi_black.png";
//                statusDir[4] = dir + "/com.android.systemui/vivo_battery_20_19_color.png";;
//                isDark = false;
//            }
//            //String colorname = new MainListenController().getXmlvalue(dir + "/vivo\\theme_values.xml ","vivo_window_statusbar_bg_color");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return isDark;
//    }

    public boolean checkDark(String dir) {
        for (int i = 0; i < 9; i++) {
            statusDir[i] = dir + "/" + ResName.firstPicResNames[i +6 ];
        }
        boolean isDark = false;
        try {
            if (IsDark.getRGB(ImageIO.read(new File(dir + "/" + ResName.firstPicResNames[14])), 10, 10)) {

                statuDir[0] = ShowView.class.getResourceAsStream("statue/signal_light.png");
                statuDir[1] = NinePatch.class.getResourceAsStream("statue/time_light.png");
                statuDir[2] = ReadXML.class.getResourceAsStream("statue/vivo_signal4_small_4_white.png");
                statuDir[3] = ResName.class.getResourceAsStream("statue/wifi_light.png");
                statuDir[4] = new FileInputStream(dir + "/com.android.systemui/vivo_battery_20_19_white.png");
                statusDir[4] = dir + "/com.android.systemui/vivo_battery_20_19_white.png";
                isDark = true;
            } else {
                statusDir[0] = "src/com/statue/signal_black.png";
                statusDir[1] = "src/com/statue/time_black.png";
                statusDir[2] = "src/com/statue/vivo_signal4_small_4_color.png";
                statusDir[3] = "src/com/statue/wifi_black.png";

                statuDir[0] = ShowView.class.getResourceAsStream("statue/signal_black.png");
                statuDir[1] = ShowView.class.getResourceAsStream("statue/time_black.png");
                statuDir[2] = ShowView.class.getResourceAsStream("statue/vivo_signal4_small_4_color.png");
                statuDir[3] = ShowView.class.getResourceAsStream("statue/wifi_black.png");
                statuDir[4] = new FileInputStream(dir + "/com.android.systemui/vivo_battery_20_19_color.png");
                statusDir[4] = dir + "/com.android.systemui/vivo_battery_20_19_color.png";;
                isDark = false;
            }
            //String colorname = new MainListenController().getXmlvalue(dir + "/vivo\\theme_values.xml ","vivo_window_statusbar_bg_color");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isDark;
    }

    public String[] getXmlvalue(String xmlname,String key){

        String value = ReadXML.getThemeValue(xmlname).get(key);
//        System.out.println(value);
        String colorvalue = "0x" + value.substring(3,value.length());
        String opca = value.substring(1,3);
        String va[] = {colorvalue,opca};
        return va;
    }
    public String getXmlvaluename(String xmlname,String key){

        String value = ReadXML.getThemeValue(xmlname).get(key);

        return value;
    }
}
